/**
 ******************************************************************************
 * @file    crc.h
 * @author  hyseim software Team
 * @date    13-Mar-2024
 * @brief   This file provides all the headers of the crc functions.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2020 Hyseim. Co., Ltd.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __CRC_H__
#define __CRC_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "chip_define.h"
#include "utils.h"

/* ================================================================================ */
/* ================          Cyclic Redundancy Check (CRC)         ================ */
/* ================================================================================ */
typedef struct
{
  __IO uint32_t DATA;            /*!< CRC Data register,                 Address offset: 0x000 */
  __IO uint32_t GPOLY;           /*!< CRC Gpoly register,                Address offset: 0x004 */
  __IO uint32_t CTRL;            /*!< CRC Ctrl register,                 Address offset: 0x008 */
  __IO uint32_t SEED;            /*!< CRC Initial Seed register,         Address offset: 0x00C */
  __IO uint32_t OXOR;            /*!< CRC Initial XOR register,          Address offset: 0x010 */  
} CRC_t;

#define CRC          ((CRC_t *) CRC_BASE)
/* ================================================================================ */
/* ================          Cyclic Redundancy Check (CRC)         ================ */
/* ================================================================================ */
/********************************  Bit definition for CRC_CTRL register  ********************************/
#define CRC_CTRL_CRC_EN                          (0x1U << 0)          /*!< CRC enable */

#define CRC_CTRL_TCRC_MASK                       (0x3U << 24)         /*!< CRC protocol Width field mask bit */
#define CRC_CTRL_TCRC_16CRC                      (0x0U << 24)         /*!< 16 bit width CRC */
#define CRC_CTRL_TCRC_32CRC                      (0x1U << 24)         /*!< 32 bit width CRC */
#define CRC_CTRL_TCRC_8CRC                       (0x2U << 24)         /*!< 8 bit width CRC */

#define CRC_CTRL_OXOR_EN                         (0x1U << 26)         /*!< CRC XOR enable */

#define CRC_CTRL_TOTR_TRANSPOSE_MASK             (0x3U << 28)         /*!< CRC type of transpose for read field mask bit */
#define CRC_CTRL_TOTR_TRANSPOSE_NONE             (0x0U << 28)         /*!< The transpose type read is no transposition */
#define CRC_CTRL_TOTR_TRANSPOSE_BITS             (0x1U << 28)         /*!< The transpose type read is bits in bytes are transposed */
#define CRC_CTRL_TOTR_TRANSPOSE_BITS_AND_BYTES   (0x2U << 28)         /*!< The transpose type read is both bits in bytes and bytes are transposed */
#define CRC_CTRL_TOTR_TRANSPOSE_BYTES            (0x3U << 28)         /*!< The transpose type read is only bytes are transposed */

#define CRC_CTRL_TOT_TRANSPOSE_MASK              (0x3U << 30)         /*!< CRC type of transpose for writes field mask bit */
#define CRC_CTRL_TOT_TRANSPOSE_NONE              (0x0U << 30)         /*!< The transpose type written is no transposition */
#define CRC_CTRL_TOT_TRANSPOSE_BITS              (0x1U << 30)         /*!< The transpose type written is bits in bytes are transposed */
#define CRC_CTRL_TOT_TRANSPOSE_BITS_AND_BYTES    (0x2U << 30)         /*!< The transpose type written is both bits in bytes and bytes are transposed */
#define CRC_CTRL_TOT_TRANSPOSE_BYTES             (0x3U << 30)         /*!< The transpose type written is only bytes are transposed */

/** @addtogroup IM110GW_CRC_Driver
  * @{
  */

/** @addtogroup CRC
  * @{
  */

/* Exported types ------------------------------------------------------------*/

/** 
  * @brief  CRC Init Structure definition  
  */
typedef struct
{
    uint32_t CRC_Gpoly;      /*!< Polynomial of CRC */

    uint32_t CRC_Seed;       /*!< The initial seed value of CRC */

    uint32_t CRC_Oxor;       /*!< The initial XOR value of CRC */

    uint32_t CRC_BitWidth;   /*!< The data width of CRC 
                                  This parameter can be a value of @ref CRC_BitWidth */

    uint32_t CRC_OxorMode;   /*!< Does CRC perform XOR operation
                                  This parameter can be a value of @ref CRC_XorEnable */
    
    uint32_t CRC_TotMode;    /*!< CRC input data transpose type 
                                  This parameter can be a value of @ref CRC_WriteTranspose */

    uint32_t CRC_TotrMode;   /*!< CRC output data transpose type 
                                  This parameter can be a value of @ref CRC_ReadTranspose */
} CRC_Init_t;

/* Exported constants --------------------------------------------------------*/

/** @defgroup CRC_Exported_Constants
  * @{
  */

/** @defgroup CRC_BitWidth 
  * @{
  */
#define CRC_BitWidth_8b             CRC_CTRL_TCRC_8CRC
#define CRC_BitWidth_16b            CRC_CTRL_TCRC_16CRC
#define CRC_BitWidth_32b            CRC_CTRL_TCRC_32CRC
/**
  * @}
  */


/** @defgroup CRC_WriteTranspose 
  * @{
  */
#define CRC_WriteTranspose_None              CRC_CTRL_TOT_TRANSPOSE_NONE
#define CRC_WriteTranspose_Bits              CRC_CTRL_TOT_TRANSPOSE_BITS
#define CRC_WriteTranspose_BitsAndBytes      CRC_CTRL_TOT_TRANSPOSE_BITS_AND_BYTES
#define CRC_WriteTranspose_Bytes             CRC_CTRL_TOT_TRANSPOSE_BYTES
/**
  * @}
  */


/** @defgroup CRC_ReadTranspose 
  * @{
  */
#define CRC_ReadTranspose_None              CRC_CTRL_TOTR_TRANSPOSE_NONE
#define CRC_ReadTranspose_Bits              CRC_CTRL_TOTR_TRANSPOSE_BITS
#define CRC_ReadTranspose_BitsAndBytes      CRC_CTRL_TOTR_TRANSPOSE_BITS_AND_BYTES
#define CRC_ReadTranspose_Bytes             CRC_CTRL_TOTR_TRANSPOSE_BYTES
/**
  * @}
  */


/** @defgroup CRC_XorEnable 
  * @{
  */
#define CRC_Xor_Enable                      CRC_CTRL_OXOR_EN
#define CRC_Xor_Disable                     (0x0U << 26)
/**
  * @}
  */

/**
  * @}
  */

/* Exported macro ------------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/

void CRC_SetXor(uint32_t bitwidth, uint32_t totr, uint32_t xor_value);
void CRC_Init(CRC_Init_t *CRC_InitStruct);
void CRC_StructInit(CRC_Init_t* CRC_StructInit);
uint32_t CRC_GetBitWidth(void);
uint32_t CRC_GetWriteTranspose(void);
uint32_t CRC_GetReadTranspose(void);
uint8_t CRC8_Calculate(const uint8_t *ptr_data, uint32_t data_len);
uint16_t CRC16_Calculate(const uint16_t *ptr_data, uint32_t data_len);
uint32_t CRC32_Calculate(const uint32_t *ptr_data, uint32_t data_len);

/**
  * @}
  */

/**
  * @}
  */

#ifdef __cplusplus
}
#endif

#endif
